<?php

class CopiesController extends AppController {

    var $name = 'Copies';
    
    function beforeFilter() {
        parent::beforeFilter();
        
        //si no es un usuario con nivel = 3 o 1 no lo dejo acceder a las siguientes funciones de este controlador
        if (in_array($this->action, array('index','add', 'edit', 'delete', 'view')) and $this->Auth->user('id') and !(in_array($this->Auth->user('level'), array(1,3)))) {
            $this->redirect(array('user' => false, 'controller' => 'articles', 'action' => 'inicio'));
        }
    }

    function index() {
        $this->Copy->recursive = 0;
        $this->paginate['order'] = array('Copy.date' => 'ASC');
        $this->set('copies', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Copia invalida', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('copy', $this->Copy->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->Copy->create();
            if ($this->Copy->save($this->data)) {
//                $this->Session->setFlash(__('La copia fue guardada.', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('La copia no fue guardada. Por favor, intente de nuevo.', true));
            }
        }
        $keys = $this->Copy->Key->find('list');
        $this->set(compact('keys'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Copia invalida', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            $datos = $this->data;
//                    pr($datos);die;
            list($d, $m, $a) = explode("/", $datos['Copy']['date']);
            $fecha_aux = $a . "-" . $m . "-" . $d;
            $datos['Copy']['date'] = $fecha_aux;
            if ($this->Copy->save($datos['Copy'])) {
//                $this->Session->setFlash(__('La copia fue guardada.', true));
                $this->redirect(array('controller' => 'keys', 'action' => 'view', $this->data['Copy']['key_id']));
            } else {
                $this->Session->setFlash(__('La copia no fue guardada. Por favor, intente de nuevo.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Copy->read(null, $id);
            $this->data['Copy']['date'] = date('d/m/Y', strtotime($this->data['Copy']['date']));
        }
        $keys = $this->Copy->Key->find('list');
        $this->set(compact('keys'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Copia invalida', true));
            $this->redirect(array('action' => 'index'));
        }
        $datos = $this->Copy->find('first', array('conditions' => array('Copy.id' => $id)));
        if ($this->Copy->delete($id)) {
            $this->Session->setFlash(__('Copia eliminada', true));
            $this->redirect(array('controller' => 'keys', 'action' => 'view', $datos['Copy']['key_id']));
        }
        $this->Session->setFlash(__('Copia no eliminada', true));
        $this->redirect(array('controller' => 'keys', 'action' => 'view', $datos['Copy']['key_id']));
    }

}
